Repository navigation
fix(gorm): compile a JPA entity with several @Id properties - #16990
borinquenkid wants to merge 3 commits into
Conversation
The dirty checking transformation added a getId() accessor for every @id property not named id, so an entity with a composite id got the same method twice and failed with 'duplicates another method'. The accessor is now added only when exactly one property is the @id. Fixes #14494 Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
jamesfredley
left a comment
There was a problem hiding this comment.
Not approving. An entity whose identifiers are all declared on it compiles. Identifiers split between a mapped superclass and the entity still fail to compile.
| if (classNode.getField(GormProperties.IDENTITY) == null && gettersAndSetters[GormProperties.IDENTITY] == null) { | ||
| // if the property is the only JPA @Id but the property name is not id add a transient getter to retrieve the id called getId, | ||
| // a composite id has no single value to return | ||
| if (jpaIdPropertyCount == 1 && classNode.getField(GormProperties.IDENTITY) == null && gettersAndSetters[GormProperties.IDENTITY] == null) { |
There was a problem hiding this comment.
A mapped superclass has @id Long orgId. The entity extends it and adds @id String userNo. The count sees only userNo, so it still generates getId() on both classes, with different return types. Compilation fails. Count inherited identifiers too, and do not generate getId() when there is more than one.
…whether to generate getId Answers the review of #16990: an entity whose @id properties are split between a mapped superclass and the entity counted only its own, so it still generated getId() beside the superclass's, with a different return type. The table spec covers where the identifiers are declared. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
Answers the split-identifier case. The count that decides whether to generate
Not tried: a superclass that comes from a precompiled jar, whose annotations the compiler reads differently. |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. Identifiers split between a mapped superclass and the entity no longer generate two getId methods. An ordinary superclass that has an identifier is still counted, so an entity with one identifier loses its getter.
| */ | ||
| private static int inheritedJpaIdCount(ClassNode classNode) { | ||
| Set<String> names = new LinkedHashSet<String>() | ||
| for (ClassNode superClass = classNode.superClass; superClass != null && superClass != ClassHelper.OBJECT_TYPE; superClass = superClass.superClass) { |
There was a problem hiding this comment.
A plain superclass has an identifier field, and the entity has one identifier of its own. The entity used to get a getter. It does not now, because this counts the superclass field even though that class is not mapped. Count a superclass only when its mapping is kept.
…g is kept The count of identifiers that decides whether an entity gets a generated getId() also counted a plain superclass that declares an @id, which no mapping keeps. An entity with one identifier of its own then lost its getter, as if it had two. Only a mapped superclass or an entity superclass is counted now, at any height of the hierarchy, a plain class in between being skipped. JpaCompositeIdSpec gains rows for a plain superclass with one and with two identifiers, with one on a getter, an entity superclass, and a mapped superclass above a plain class. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
Thank you, you are right: the count took a plain superclass's Only a superclass whose mapping is kept is counted now: a
|
✅ All tests passed ✅🏷️ Commit: 159d074 Learn more about TestLens at testlens.app/docs. |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. An ordinary superclass is no longer counted. The same identifier declared again on the entity is counted twice, so the getter is not generated.
| } | ||
| } | ||
| // an identifier declared on a superclass belongs to the same key, so it counts as well | ||
| jpaIdPropertyCount += inheritedJpaIdCount(classNode) |
There was a problem hiding this comment.
A mapped superclass declares getCode, and the entity declares the same identifier again. The entity used to get a getter. It does not now, because the same name is counted twice. Count each name once.
DirtyCheckingTransformeradded a transientgetId()accessor for every JPA@Idproperty whose name is notid. An entity with two@Idproperties (a composite id) got the method twice and failed to compile with "The method public java.lang.Long getId() duplicates another method of the same signature". The accessor is now added only when exactly one property is the@Id, since a composite id has no single value to return.Verified locally: the new
JpaCompositeIdSpecentity failed to compile with the three errors from the issue before the change; now it compiles, saves and loads by its composite id. A second feature keeps the single-@Idcase working (getId()still returns the@Idvalue). All 252grails-datamapping-coretests and the Hibernate 5grails.gorm.tests.jpaspecs pass; CodeNarc is clean.Fixes #14494
https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL